package database

import (
	"fmt"

	"example.com/go-vue.git/common/log"
	"example.com/go-vue.git/config"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/postgres"
	"github.com/sirupsen/logrus"
)

var DB *gorm.DB
var err error

func Init() {
	conn := config.GetConfig().Database.Conn
	port := config.GetConfig().Database.Port
	user := config.GetConfig().Database.User
	password := config.GetConfig().Database.Password
	tabel := config.GetConfig().Database.Tabel
	DB, err = gorm.Open("postgres", fmt.Sprintf("host=%s port=%s user=%s dbname=%s password=%s sslmode=disable", conn, port, user, tabel, password))
	if err != nil {
		log.Db("数据库启动失败", logrus.Fields{"conn": conn, "port": port, "user": user, "password": password, "tabel": tabel})
		return
	}
	// 模型创建数据库表
	// DB.AutoMigrate(&models.Cat{})
}
